from selenium import webdriver
import time


class Mail163(object):
    def __init__(self):
        """打开浏览器"""
        self.driver = webdriver.Chrome()

    def open_email(self, url):
        """传入网易邮箱url"""
        self.driver.get(url)
        # self.driver.maximize_window()  # 让浏览器最大化
        time.sleep(1)

    def login_email(self, email, password):
        """输入账号和密码和iframe的处理"""

        # 定位iframe
        iframe = self.driver.find_element_by_xpath('//div[@id="loginDiv"]/iframe[@scrolling = "no"]')

        # switch_to.frame进入到iframe里面去
        self.driver.switch_to.frame(iframe)

        # 输入邮箱号
        self.driver.find_element_by_xpath('//input[@name="email"]').send_keys(email)

        # 输入密码
        self.driver.find_element_by_xpath('//div[@class="u-input box"]//input[@name="password"]').send_keys(password)

        # 点击登录
        self.driver.find_element_by_xpath('.//*[@id="dologin"]').click()

    def close(self):
        """关闭浏览器"""
        time.sleep(10)
        self.driver.quit()


wy = Mail163()
wy.open_email('https://mail.163.com/')
wy.login_email('你的email', '你的密码')
wy.close()
